package com.test.ps;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.swing.text.html.HTMLDocument.Iterator;

public class DataBean {

	private static CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<Integer>();
	
	ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
	
	
	public static void addList(Integer data) {
		list.add(data);
		System.out.println("生产中list的值"+list.size());
	}
	
	@SuppressWarnings("unchecked")
	public static List<Integer> getList(){
		List<Integer> data = new ArrayList<Integer>();
		if(list != null) {
			System.out.println("list中的值"+list.size());
			data = (List<Integer>) list.clone();
			System.out.println(data.size());
			list.clear();
		}
		return data;
	}
	
	private volatile boolean flag = true;
	
	public void addMap(String code) {
		while(!flag) {
			
		}
		int num = 1;
		if(map.containsKey(code)) {
			num = map.get(code);
			num++;
		}
		map.put(code, num);
		System.out.println("-----"+map.get("wuyu"));
	}
	
	public Map<String,Integer> getMap() {
		Map<String,Integer> m = new HashMap<String, Integer>();
		Set<Entry<String,Integer>> entrySet = map.entrySet();
		if(entrySet != null && entrySet.size() > 0) {
			this.flag = false;
			m = this.mapCopy(entrySet, m);
			map.clear();
			this.flag = true;
		}
		return m;
	}
	
	/*
	 * public boolean isFlag() { return flag; }
	 * 
	 * public void setFlag(boolean flag) { this.flag = flag; }
	 */
	public Map<String,Integer> mapCopy(Set<Entry<String,Integer>> entrySet, Map<String,Integer> resultMap) {
		for (Entry<String, Integer> entry : entrySet) {
			resultMap.put(entry.getKey(), entry.getValue());
		}
		return resultMap;
	}
	
}
